%Reduction Error Analysis for Correction Constant
clc;
close all;

%% Plot the average (expected) error against probability
m = 6; n = 6; r = 6; k = 2;
ulp = 2^(-6);
alpha = 0:0.01:1;
L = length(alpha);
err_alpha = zeros(1, L);
for i = 1:L
    i_alpha = alpha(i);
    err_alpha(i) = ccm_rd(m, n, r, k, i_alpha);
end
figure(1);
%err_alpha = err_alpha./ulp;
plot(alpha, err_alpha);
title('Sum of Truncated Columns vs Prior');
xlabel('Prior');
ylabel('Sum(ulp)');

%%
abs_opt_err_alpha = zeros(1, L);
abs_uniform_err_alpha = zeros(1, L);
const_uniform = ccm_rd(m, n, r, k, 0.5);
const_opt = 0.5 * ccm_rd(m, n, r, k, 1);
for i = 1:L
    i_alpha = alpha(i);
    abs_opt_err_alpha(i) = abs(err_alpha(i) - const_opt);
    abs_uniform_err_alpha(i) = abs(err_alpha(i) - const_uniform);
end
figure(2);
plot(alpha, abs_uniform_err_alpha./ulp, alpha, abs_opt_err_alpha./ulp);
legend('Uniform', 'Optimal');
title('Minimax Reduction Error');
xlabel('Prior');
ylabel('Error(ulp)');